package com.wan.uitls

import android.util.Log
import com.wan.base.WanApp
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Response
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class RetrofitHelper private constructor() {
    private val mThreadName = this.javaClass.name

    private var map = mutableMapOf<String, ArrayList<Call<*>>>()

    private val client: OkHttpClient = OkHttpClient.Builder().build()

//    //Kotlin实现 单例实现
//    class SingletonDemo private constructor() {
//        companion object {
//            private var instance: SingletonDemo? = null
//                get() {
//                    if (field == null) {
//                        field = SingletonDemo()
//                    }
//                    return field
//                }
//            fun get(): SingletonDemo{
//                //细心的小伙伴肯定发现了，这里不用getInstance作为为方法名，是因为在伴生对象声明时，内部已有getInstance方法，所以只能取其他名字
//                return instance!!
//            }
//        }
//    }

    companion object {
        private var instance: RetrofitHelper? = null
            get() {
                if (field == null) {
                    field = RetrofitHelper()

                }
                return field
            }

        fun get(): RetrofitHelper {
            return instance!!
        }

        private var apiServices: APIServices? = null
    }

    init {
//        //添加 header
//        client.interceptors().add(Interceptor {
//            if (CommonUtils.getLoginState(WanApp.sContext)) {
//                var request = it.request()
//                var addHeader = request.newBuilder()
//                        .addHeader(CommonUtils.Cookie_Send, CommonUtils.getLogginMessage(WanApp.sContext)!!.token)
//                it.proceed(addHeader.build())
//            } else {
//                var request = it.request()
//                var response = it.proceed(request)
//                response
//            }
//
//        })
        if (apiServices == null) {
            val retrofit = Retrofit.Builder()
                    .baseUrl(APIServices.BASE_URL)
//                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build()
            apiServices = retrofit.create(APIServices::class.java)
        }
        Log.d(mThreadName, "init : 初始化执行几次")
    }

    fun getApiServices(): APIServices? {
        return apiServices
    }

    fun addCall(tag: Any, call: Call<*>) {
        var name = tag.javaClass.name

        Log.d(mThreadName, "add Name : $name")
        if (map.containsKey(name)) {
            var arrayList = map[name]
            arrayList!!.add(call)
            Log.d(mThreadName, "addCall : 多个请求")
        } else {
            var arrayList = ArrayList<Call<*>>()
            arrayList.add(call)
            map[name] = arrayList
            Log.d(mThreadName, "addCall : 添加一个新请求")
        }
    }

    fun cancelCall(tag: Any) {
        var name = tag.javaClass.name
        Log.d(mThreadName, "cancel Name : $name")
        if (map.containsKey(name)) {
            var callList = map[name]
            if (callList!!.isNotEmpty()) {
                callList.filter {
                    Log.d(mThreadName, "cancelCall : 取消请求${it.request().url()}")
                    if (it.isExecuted && !it.isCanceled) {
                        it.cancel()
                    }
                    true
                }
            }
            map.remove(name)
        } else {
            Log.d(mThreadName, "cancelCall : 未发现该请求")

        }
    }

    fun cancelAllCall() {
        if (map.isNotEmpty()) {
            map.entries.forEach {
                var callList = it.value
                if (callList.isNotEmpty()) {
                    callList.forEach { call ->
                        if (call.isExecuted && !call.isCanceled) {
                            call.cancel()
                        }
                    }
                }
            }
            map.clear()
        }
    }

    fun getAllCall() {
        if (map.isNotEmpty()) {
            map.entries.forEach {
                var name = it.key
                it.value.forEach { call ->
                    Log.d(mThreadName, "$name : ${call.request().url()}")
                }
            }
        } else {
            Log.d(mThreadName, "getAllCall : map is null")
        }
    }

}